package com.taoban.shopdata.thread;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.taoban.dao.HiberOperations;
import com.taoban.dao.mongodb.MongoDb;
import com.taoban.dao.mongodb.MongoDbOps;
import com.taoban.model.spider.MallItem;
import com.taoban.model.spider.MallItemDesc;
import com.taoban.shopdata.entity.TMallCrawlerConfig;
import com.taoban.shopdata.entity.TMallCrawlerConfigList;
import com.taoban.shopdata.utils.ConfigUtils;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.ItemsListGetRequest;
import com.taobao.api.response.ItemsListGetResponse;
import com.yufei.entity.PaginationRule;
import com.yufei.entity.UrlParameter;
import com.yufei.utils.CommonUtil;
import com.yufei.utils.ExceptionUtil;
import com.yufei.utils.PatternUtils;
import com.yufei.utils.XPathUtil;

/**
 * @author jasstion
 * 下午5:33:53
 * 利用淘宝提供的商品接口来获取所有的商品数据并且保存进数据库
 * 
 * 
 */
public class TMallProductCollectThread implements Runnable{
	private static Log mLog = LogFactory.getLog(TMallProductCollectThread.class);
	private final String   url = "http://gw.api.taobao.com/router/rest";
	private HiberOperations ho=new MongoDbOps();
    private  List<TMallCrawlerConfig> loadTMallCrawlerConfig(){
    	InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("tMallCrawlerConfigList.xml");
    	TMallCrawlerConfigList tMallCrawlerConfigList=(TMallCrawlerConfigList) CommonUtil.getObjectFromXml(in, TMallCrawlerConfigList.class);
    			
    	return tMallCrawlerConfigList.gettMallCrawlerConfig();
    }
    @Override
	public void run() {
		// TODO Auto-generated method stub
          long begainTime=System.currentTimeMillis();
    	 List<TMallCrawlerConfig> tMallCrawlerConfigs=this.loadTMallCrawlerConfig();
    	 if(CommonUtil.isEmptyOrNull(tMallCrawlerConfigs)){
    		 mLog.error("请正确的初始化配置文件!");
    		 
    	 }
    	 List<String> fetchMallItemIds =null;
    	 for(TMallCrawlerConfig tMallCrawlerConfig:tMallCrawlerConfigs){
    		 fetchMallItemIds = this.fetchMallItemIds(tMallCrawlerConfig);
    		 if(CommonUtil.isEmptyOrNull(fetchMallItemIds)){
    			 continue;
    		 }
			getMallItemDetailsAndImportProductDB(fetchMallItemIds,tMallCrawlerConfig);
    	 }
         long endTime=System.currentTimeMillis();
         mLog.info("此次运行共用时:"+(endTime-begainTime)/1000/60+"分钟");

    	 
	}
public void getMallItemDetailsAndImportProductDB(List<String> itemIds,TMallCrawlerConfig tMallCrawlerConfig){
	//每个程序仅仅使用一个账户
	  String fields="num_iid,item_img,props_name,title,price,desc,url,detail_url,title,nick,desc,cid,pic_url,num,valid_thru";
      String appkey =ConfigUtils.getVlaue("//tianmall/appKey");
      String appSecret = ConfigUtils.getVlaue("//tianmall/appSecret");
	  TaobaoClient client=new DefaultTaobaoClient(url, appkey, appSecret,"xml");
	 
	  List<List<String>> itemIdGroups=CommonUtil.splitKs(itemIds, 10);
	  mLog.info("此次处理将"+itemIds.size()+"按照10分成"+itemIdGroups.size()+"个批次分别进行处理!");
	//  req.setTrackIid("20799800694_track_11116");
	for(List<String> itemIds1:itemIdGroups){
		 try {
			  ItemsListGetRequest req=new ItemsListGetRequest();
			  req.setFields(fields);
			  req.setNumIids(CommonUtil.LinkStringWithSpecialSymbol(itemIds1, ","));
			ItemsListGetResponse response = client.execute(req);
			String xmlResponse=response.getBody();
			//将数据获取保存到生成数据库
			NodeList  itemNodes=XPathUtil.getNodeListtByXPath(xmlResponse,"//item");
            Node itemNode=null;
            MallItem mallItem=null;
           for(int i=0;i<itemNodes.getLength();i++){
        	   try{
        			   
        			   itemNode=itemNodes.item(i);
                	   mallItem=new MallItem();
                	   mallItem.setItemUrl(XPathUtil.getValueOfNode(itemNode, "detail_url"));
                	   //天猫mallId
                	   int mallId = 50;
					   mallItem.setMallId(mallId);
                	   String mallItemId = XPathUtil.getValueOfNode(itemNode, "num_iid");
					   mallItem.setMallItemId(mallItemId);
                	   mallItem.setPrice(Float.parseFloat(XPathUtil.getValueOfNode(itemNode, "price")));
                	   mallItem.setDataTime(System.currentTimeMillis());
                	   int stockAmount = Integer.parseInt(XPathUtil.getValueOfNode(itemNode, "num"));
					   mallItem.setStockAmount(stockAmount);
                	   //设置是商品分类id
                	 //  mallItem.setCatalogId(tMallCrawlerConfig.getCatalogId());
                	   mallItem.setPictureUrl(XPathUtil.getValueOfNode(itemNode, "pic_url"));
                	   mallItem.setStockStatus(stockAmount<=0?"无货":"有货");
                	   mallItem.setId(MongoDb.getInstance().getLongAutoId(MallItem.class));
                	   mallItem.setTitle(XPathUtil.getValueOfNode(itemNode, "title"));
                	   long id= (Long) ho.save(mallItem);
                	  
                	   MallItemDesc mallItemDesc=new MallItemDesc();
                	   mallItemDesc.setId(MongoDb.getInstance().getLongAutoId(MallItemDesc.class));
                	   mallItemDesc.setItemId(id);
                	   mallItemDesc.setMallId(mallId);
                	   mallItemDesc.setMallItemId(mallItemId);
                	   mallItemDesc.setDescription(XPathUtil.getValueOfNode(itemNode, "desc"));
                	   ho.save(mallItemDesc);
        	   }catch(Exception e){
       			mLog.info(ExceptionUtil.getExceptionDetailsMessage(e));

        	   }
        	
        	   
           }
			
		} catch (ApiException | XPathExpressionException | ParserConfigurationException | SAXException | IOException e) {
			// TODO Auto-generated catch block
			mLog.info(ExceptionUtil.getExceptionDetailsMessage(e));
		}
	}
	 
	
}
private List<String> fetchMallItemIds(TMallCrawlerConfig tMallCrawlerConfig){
	if(!TMallCrawlerConfig.isValid(tMallCrawlerConfig)){
	mLog.info(("tMallCrawlerConfig配置有误！直接忽略此tMallCrawlerConfig的信息采集！"));
	return null;
	}
	List<String> mallItemIds=new ArrayList<>();
	
	
	
	String keyWordSearchResponse=null;
	try {
		keyWordSearchResponse=CommonUtil.getHtmlContent(tMallCrawlerConfig.getSearchUrl(), false);
		String maxPageNumberStr=PatternUtils.getStrByRegex(keyWordSearchResponse,tMallCrawlerConfig.getPageTotalNumberRegex());
		int maxPageNumber=-1;
		mLog.debug(keyWordSearchResponse);
		if(!CommonUtil.isEmptyOrNull(maxPageNumberStr)){
			maxPageNumber = Integer.parseInt(maxPageNumberStr.substring(maxPageNumberStr.lastIndexOf("/")+1));
			mLog.info("获取页数为"+maxPageNumber+"页");

		}
		if(maxPageNumber==-1){
			mLog.info("获取关键词对应搜索记录总分页数失败，默认为30页");

		}
		String pageUrlTemplate=tMallCrawlerConfig.getPageUrlTemplate();
		PaginationRule paginationRule = new PaginationRule();
		    paginationRule.setPaginationTemplate(pageUrlTemplate);
		    UrlParameter urlParameter=new UrlParameter();
		    urlParameter.setBegainPagNumber(1);
		    urlParameter.setPagNumberSpace(1);
		    urlParameter.setParameterType("Integer");
		    urlParameter.setParameterValue(String.valueOf(maxPageNumber));
		    paginationRule.getUrlParameters().add(urlParameter);
			List<String> urls=CommonUtil.generateUrlsByPaginationRule(paginationRule);
			String pageUrlContent=null;
			 int totalNumbers=0;
			
			for(String pageUrl:urls){
				pageUrlContent=CommonUtil.getHtmlContent(pageUrl, false);
				 
				   List<String> tempMallItemIds=PatternUtils.getListStrByRegex(pageUrlContent, tMallCrawlerConfig.getItemIdRegex());
				   
				   totalNumbers+=tempMallItemIds.size();
				    for(String mallId:tempMallItemIds){
						mLog.info("mallItemId:"+mallId+"");

				    }
				    mallItemIds.addAll(tempMallItemIds);
			}
			mLog.info(totalNumbers);

	} catch (IOException e) {
		// TODO Auto-generated catch block
		mLog.info(ExceptionUtil.getExceptionDetailsMessage(e));
	}
	return mallItemIds;
}
public static void main(String[] args){
	//获取淘宝某个分类下的所有商品的itemId
	//根据这些itemIds来调用淘宝接口获取对应商品信息
	//根据itemId获取对应商品的评论信息
	

Thread thread=new Thread(new TMallProductCollectThread());
thread.start();


}
}
